import './App.css'
import EntityFiles from './main/index';
import React, { useEffect, useState } from 'react';

declare global {
  interface Window {
    IframeProps?: any;
  }
}
function App() {
  const [params, setParams] = useState<any>(null);
  const [finished, setFinished] = useState<() => void>(()=>{});
  const [getHtml, setGetHtml] = useState<() => void>(()=>{});
  const audioExt = ['.mp3', '.wav', '.ogg'];
  const videoExt = ['.mp4', '.avi', '.mov', '.mpg', '.swf', '.flv', '.mpeg'];
  // 子窗口代码
  useEffect(() => {
    const parentProps = window.parent?.IframeProps;
    if (parentProps) {
      const { entity, finished, getHtml, shareOpenLink } = parentProps;
      const data = 'size' in entity ? entity : entity.filedata;
  
      const isVideo =
      data.contentType?.startsWith('video') || videoExt.includes(data.extension ?? '-');
      const isImage = data.contentType?.startsWith('image');
      const isAudio =
      data.contentType?.startsWith('audio') || audioExt.includes(data.extension ?? '-');
  
      const config = {
        type: isImage ? '.image' : isVideo ? '.video' : isAudio ? '.audio' : data.extension,
        name: data.name,
        link: shareOpenLink(data.shareLink),
        shareFile: data,
        poster:shareOpenLink(data.poster),
        contentType:data.contentType
      };
  
      setParams(config);
      setFinished(() => finished);
      setGetHtml(() => getHtml);
    } else {
      console.error('无法从父窗口获取 props');
    }
  }, []);
  
  return (
    <>
      {params&&<EntityFiles wps={false} finished={finished} getHtml={getHtml} requestType="wadouri"  {...params}/>}
    </>
  )
}

export default App
